/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddDoctorForm.java
 *
 * Created on Apr 19, 2012, 5:48:36 PM
 */
package GUI.DoctorManage;

import GUI.MainFunction;
import GUI.OptionMessage;
import connection.ManLabConnection;
import entities.DoctorDetails;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import utilities.ValidateInput;

/**
 *
 * @author Admin
 */
public class AddDoctorForm extends javax.swing.JDialog {

    private DoctorDetails doctorDetails;
    private ArrayList<DoctorDetails> doctorDetailses;
    private MainFunction parentFrame;
    private ValidateInput vi;
    private ManLabConnection cnn;
    private ResultSet rs;
    private String qry;
    private boolean isEnable = false;
    private OptionMessage optionMessage;

    /** Creates new form AddDoctorForm */
    public AddDoctorForm(java.awt.Frame parent, boolean modal, ArrayList<DoctorDetails> doctorDetailses, MainFunction parentFrame) {
        super(parent, modal);
        initComponents();
        this.doctorDetails = new DoctorDetails();
        this.doctorDetailses = new ArrayList<DoctorDetails>();
        this.doctorDetailses = doctorDetailses;
        this.parentFrame = parentFrame;
        this.vi = new ValidateInput();
        jRadioButton1.setSelected(true);
        jPasswordField2.setEnabled(false);
        jPasswordField1.setEnabled(false);
        optionMessage = new OptionMessage(this);
//        this.parentFrame.setRegister(false);
    }

    private void addDoctor() {
        try {
            String dPass = new String(jPasswordField1.getPassword());


            String dConfirm = new String(jPasswordField2.getPassword());


            String dFName = jTextField2.getText();
            if (vi.checkIsEmpty(dFName)) {
                JOptionPane.showMessageDialog(parentFrame, "First Name must filled!!!");
                jTextField2.requestFocusInWindow();
                return;
            }

            String dLName = jTextField3.getText();
            if (vi.checkIsEmpty(dLName)) {
                JOptionPane.showMessageDialog(parentFrame, "Last name must filled!!!");
                jTextField3.requestFocusInWindow();
                return;
            }

            String dAddress = jTextField4.getText();
            if (vi.checkIsEmpty(dAddress)) {
                JOptionPane.showMessageDialog(parentFrame, "Address must filled!!!");
                jTextField4.requestFocusInWindow();
                return;
            }

            String dPhone = jTextField5.getText();
            if (vi.checkIsEmpty(dPhone) || !vi.checkIsNumber(dPhone)) {
                JOptionPane.showMessageDialog(parentFrame, "Phone must filled valid!!!");
                jTextField5.requestFocusInWindow();
                return;
            }

            String dSpe = jTextField6.getText();
            if (vi.checkIsEmpty(dSpe)) {
                JOptionPane.showMessageDialog(parentFrame, "Specialization must filled!!!");
                jTextField6.requestFocusInWindow();
                return;
            }

            int bit;
            if (jRadioButton1.isSelected()) {
                bit = 1;
            } else {
                bit = 0;
            }
            if (!jRadioButton1.isSelected() && !jRadioButton2.isSelected()) {
                JOptionPane.showMessageDialog(parentFrame, "Gender must filled!!!");
                return;
            }
            cnn = new ManLabConnection();
            boolean cn = cnn.createConnection();
            if (dPass.length() > 0 && dConfirm.length() > 0) {
                if (vi.checkIsEmpty(dPass.toCharArray())) {
                    JOptionPane.showMessageDialog(parentFrame, "Password must filled!!!");
                    jPasswordField1.requestFocusInWindow();
                    return;
                }
                if (vi.checkIsEmpty(dConfirm.toCharArray()) || !vi.checkMatchPassword(dPass.toCharArray(), dConfirm.toCharArray())) {
                    JOptionPane.showMessageDialog(parentFrame, "Confirm must valid!!!");
                    jPasswordField2.requestFocusInWindow();
                    return;
                }
                qry = "insert into DoctorDetail values('" + vi.convertSpecialCharacters(dFName) + "','" + vi.convertSpecialCharacters(dLName) + "','" + vi.convertSpecialCharacters(dAddress) + "','" + dPhone + "'," + bit + ",'" + vi.convertSpecialCharacters(dSpe) + "','" + dPass + "','')";
            } else {
                qry = "insert into DoctorDetail values('" + vi.convertSpecialCharacters(dFName) + "','" + vi.convertSpecialCharacters(dLName) + "','" + vi.convertSpecialCharacters(dAddress) + "','" + dPhone + "'," + bit + ",'" + vi.convertSpecialCharacters(dSpe) + "',default,'')";
            }
            cnn.executeQuery(qry);
            qry = "select top 1 * from DoctorDetail order by dID desc";
            rs = cnn.createResultSet(qry);
            int dID = 0;
            while (rs.next()) {
                dID = rs.getInt("dID");
            }

            String username = generateUsername(dFName, dLName, dID);
            qry = "update DoctorDetail set dUsername='" + username + "' where dID=" + dID;
            boolean pass = cnn.executeQuery(qry);
            if (pass) {
                parentFrame.setLoginPass(true);
//                parentFrame.setRegister(false);
            }
            optionMessage.showMessage("Your username is: " + username);

            cnn.closeConnection();
            doctorDetails.setdID(dID);
            doctorDetails.setdUsername(username);
            doctorDetails.setPassword(dPass);
            doctorDetails.setdFName(dFName);
            doctorDetails.setdLName(dLName);
            doctorDetails.setdAddress(dAddress);
            doctorDetails.setdPhone(dPhone);
            doctorDetails.setdSpecialization(dSpe);
            if (bit == 1) {
                doctorDetails.setdGender(true);
            } else {
                doctorDetails.setdGender(false);
            }
            doctorDetailses.add(doctorDetails);
//            this.parentFrame.refreshReportTableView();
            if (parentFrame.isLoginPass()) {
                parentFrame.refreshReportTableView();
            }
            if (parentFrame.isRegistered()) {
                parentFrame.setCurrentUser(doctorDetails);
            }
            parentFrame.setLoginPass(true);
            parentFrame.setRegister(false);
            this.dispose();
        } catch (SQLException ex) {
            Logger.getLogger(AddDoctorForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String generateUsername(String fName, String lName, int id) {
        String[] lNameArray;
        String username = fName;
        lNameArray = lName.split(" ");
        for (int i = 0; i < lNameArray.length; i++) {
            username = username + lNameArray[i].substring(0, 1);
        }
        return (username + "_" + id).toLowerCase();
    }

    private void setEnableDisable() {
        if (isEnable) {
            jPasswordField1.setEnabled(false);
            jPasswordField2.setEnabled(false);
            isEnable = false;
        } else {
            jPasswordField1.setEnabled(true);
            jPasswordField2.setEnabled(true);
            isEnable = true;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jPasswordField2 = new javax.swing.JPasswordField();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add new Doctor", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), java.awt.Color.blue)); // NOI18N

        jLabel2.setText("PassWord");

        jLabel3.setText("FristName");

        jLabel4.setText("LastName");

        jLabel5.setText("Address");

        jLabel6.setText("Phone");

        jLabel7.setText("Specialization");

        jLabel8.setText("Gender");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Male");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Female");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/check.png"))); // NOI18N
        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/Cancel-16.png"))); // NOI18N
        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel9.setText("Confirm");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icons/edit.png"))); // NOI18N
        jButton3.setText("Edit Password");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addContainerGap())
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addContainerGap())
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addContainerGap())
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabel3)
                                            .addContainerGap())
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                    .addComponent(jLabel2)
                                                    .addGap(222, 222, 222))
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                    .addGap(102, 102, 102)
                                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                            .addComponent(jRadioButton1)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(jRadioButton2))
                                                        .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                                        .addComponent(jPasswordField2, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                            .addComponent(jButton1)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(jButton2))
                                                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                                        .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                                        .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jButton3)
                                            .addGap(25, 25, 25)))))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        addDoctor();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        setEnableDisable();
    }//GEN-LAST:event_jButton3ActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AddDoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AddDoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AddDoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AddDoctorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                AddDoctorForm dialog = new AddDoctorForm(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables
}
